# csky-elf-noneabiv2-gcc -o  you.elf  -Wl,-zmax-page-size=1024 -mhard-float   -mcpu=ck804ef  -mhard-float  -nostartfiles -Wl,--gc-sections -T"./sdk/ld/w800/gcc_csky.ld" -L.   -Wl,--start-group  -Wl,--end-group you.c
# csky-elf-noneabiv2-objdump -D you.elf  > you.dis

include ../../py/mkenv.mk

# 板级描述文件(GPIO)
# -include mpconfigport.mk

# qstr definitions (must come before including py.mk)
QSTR_DEFS = qstrdefsport.h

# MicroPython feature configurations
MICROPY_ROM_TEXT_COMPRESSION ?= 1

# include py core make definitions
include $(TOP)/py/py.mk

PYTHON	?= python
CROSS_COMPILE	?= csky-elf-noneabiv2-

# wm_tool的路径, 编译后程序后缀(win->.exe)
ProjName := W806
SDK_TOOL_DIR := ./sdk/tools/W806

# 在../../py/mkenv.mk里, BUILD=build
# 构建烧录镜像相关的参数, 拷贝自aft_build_project.sh
# img_type=0表示secboot, img_type=1用户自定义固件
ProjName := W806
img_type := 1
IMG_DIR := $(BUILD)/output
# secondboot的header、固件(body)存放位置
sec_img_header := 8002000
sec_img_pos := 8002400
# 程序的运行地址(OTA区是0x010000, secboot会把它搬运到运行区, 定义运行区RUN=OTA区也行, 不用搬运(烧录不含secboot的固件))
#run_img_header := 80D0000
#run_img_pos := 80D0400
run_img_header := 8010000
run_img_pos := 8010400
# 升级区(OTA), 固件会先下载到这里, 由ROM代码或secboot进行自搬运(这个不能改)
upd_img_pos := 8010000


ifeq ($(shell uname -s),"Linux")
	TOOL_EXT := 
else
	TOOL_EXT := .exe
endif

INC += -I.
INC += -I$(TOP)
INC += -I$(BUILD)
INC += -I./mod
INC += -I./crypto
INC += -I./sdk/Include
INC += -I./sdk/Include/arch/xt804
INC += -I./sdk/Include/arch/xt804/csi_core
INC += -I./sdk/Include/arch/xt804/csi_dsp
INC += -I./sdk/Include/driver

# RTOS
INC += -I./sdk/platform/component/FreeRTOS/include
INC += -I./sdk/platform/component/FreeRTOS/portable/xt804

# 汇编工具(as), 链接工具(ld)统一使用gcc, gcc会对汇编文件的宏定义展开, gcc链接程序时,会自行根据架构决定链接对应的lib(用ld,不确定连接哪个库)
LD = $(CC)
AS = $(CC)
OBJDUMP = $(CROSS_COMPILE)objdump
OBJCOPY = $(CROSS_COMPILE)objcopy

# CFLAGS = $(INC) -Wall -Werror -Wdouble-promotion -Wfloat-conversion -std=c99 $(COPT)
# LDFLAGS = -Wl,-Map=$@.map,--cref -Wl,--gc-sections

# 添加编译选项
CFLAGS += $(INC) -Wall -std=gnu99 -DGCC_COMPILE=1
CFLAGS += -mcpu=ck804ef -mhard-float -DTLS_CONFIG_CPU_XT804=1 -fdata-sections -ffunction-sections -c

# 添加连接选项
CFLAGS += -mcpu=ck804ef -mhard-float -Wl,--start-group  -Wl,--end-group
CFLAGS += -Wl,-zmax-page-size=1024 -Wl,--gc-sections -Wl,--ckmap=$(BUILD)/w806_sdk.map 

ASMFLAGS += -Wall \
	-DTLS_CONFIG_CPU_XT804=1 \
	-DGCC_COMPILE=1 \
	-mcpu=ck804ef \
	$(optimization) \
	-std=c99 \
	-c  \
	-mhard-float \
	-Wa,--gdwarf2 \
	-fdata-sections  \
	-ffunction-sections

LDFLAGS += -mcpu=ck804ef -nostartfiles -mhard-float
LDFLAGS += -Wl,-zmax-page-size=1024 -Wl,--gc-sections
LDFLAGS += -Wl,--ckmap=$(BUILD)/W806_sdk.map 
LDFLAGS += -L./sdk/lib/W806 -T ./sdk/ld/W806/gcc_csky.ld

# Tune for Debugging or Optimization
ifeq ($(DEBUG), 1)
CFLAGS += -O0 -ggdb
else
CFLAGS += -Os -DNDEBUG
CFLAGS += -fdata-sections -ffunction-sections
endif

# Flags for optional C++ source code
CXXFLAGS += $(filter-out -std=c99,$(CFLAGS))
CXXFLAGS += $(CXXFLAGS_MOD)

# Flags for user C modules
CFLAGS += $(CFLAGS_MOD)
LDFLAGS += $(LDFLAGS_MOD)

# dsp库、数学库参与链接
LIBS = -ldsp -lm -lgcc -lc


#	libc/string0.c
#	libc/printf.c 

SHARED_SRC_C += $(addprefix shared/,\
	readline/readline.c \
	runtime/pyexec.c \
	runtime/interrupt_char.c \
	runtime/sys_stdio_mphal.c \
	runtime/stdout_helpers.c \
	timeutils/timeutils.c \
	)


DRIVERS_SRC += $(addprefix drivers/, \
	bus/softspi.c \
)

	

SDK_DRIVERS_SRC_C += $(addprefix sdk/platform/drivers/,\
	wm_adc.c \
	wm_cpu.c \
	wm_dma.c \
	wm_gpio.c \
	wm_hal.c \
	wm_i2c.c \
	wm_internal_flash.c \
	wm_pmu.c \
	wm_pwm.c \
	wm_spi.c \
	wm_spi_flash.c \
	wm_tim.c \
	wm_touch.c \
	wm_uart.c \
	wm_wdg.c \
	)
	
	
MOD_SRC_C += $(addprefix mod/,\
	modutime.c \
	modmachine.c \
	machine_pin.c \
	machine_uart.c \
	machine_i2c.c \
	machine_i2s.c \
	machine_adc.c \
	machine_sd.c \
	machine_timer.c \
	machine_rtc.c \
	machine_wdt.c \
	machine_pwm.c \
	machine_rtc.c \
	machine_spi.c \
	moduos.c \
	)
	
CRYPTO_SRC_C += 
#$(addprefix crypto/, \
#	wm_crypto_hard.c \
#	)
	
SRC_C = \
	main.c \
	pytask.c \
	mphalport.c \
	xt804_msp.c \
	xt804_it.c \
	storage.c \
	factoryreset.c


EXTMOD_SRC_C = $(addprefix extmod/,\
	utime_mphal.c  \
	vfs.c \
	vfs_blockdev.c \
	vfs_lfs.c \
	vfs_reader.c \
	moductypes.c \
	moduzlib.c \
	modujson.c \
	modure.c \
	machine_mem.c \
	machine_spi.c \
	)

SRC_C += $(SRC_MOD)
SRC_CXX += $(SRC_MOD_CXX)

# 拷贝自stm32的Makefile
SRC_QSTR += $(SRC_C) $(SRC_CXX) $(SRC_MOD) $(SRC_MOD_CXX) $(SHARED_SRC_C) $(MOD_SRC_C) $(EXTMOD_SRC_C) $(DRIVERS_SRC)


STARTUP_ASM_SRC +=  $(addprefix ./sdk/platform/,\
    arch/xt804/bsp/startup.S \
    arch/xt804/bsp/vectors.S \
	component/FreeRTOS/portable/xt804/cpu_task_sw.S \
    )
  
RTOS_SRC += $(addprefix ./sdk/platform/component/FreeRTOS/,\
    portable/MemMang/heap_5.c \
    portable/xt804/port.c \
	croutine.c \
	event_groups.c \
	list.c \
	queue.c \
	stream_buffer.c \
	tasks.c \
	timers.c \
    )
    
BSP_SRC += $(addprefix ./sdk/platform/arch/xt804/,\
    bsp/board_init.c \
    bsp/system.c \
    bsp/trap_c.c \
	libc/libc_port.c \
    )
    
LIB_SRC += $(addprefix lib/, \
	littlefs/lfs2.c \
	littlefs/lfs2_util.c \
)

	
# 参与编译的源文件(_frozen_mpy.py编译成冻结固件, 可以不要)
OBJ += $(BUILD)/_frozen_mpy.o 
OBJ += $(PY_CORE_O) $(addprefix $(BUILD)/, $(SRC_C:.c=.o))
OBJ += $(addprefix $(BUILD)/, $(DRIVERS_SRC:.c=.o))

OBJ += $(addprefix $(BUILD)/, $(SRC_CXX:.cpp=.o))
OBJ += $(addprefix $(BUILD)/, $(SRC_CXX:.cpp=.o))
OBJ += $(addprefix $(BUILD)/, $(SHARED_SRC_C:.c=.o))
OBJ += $(addprefix $(BUILD)/, $(MOD_SRC_C:.c=.o))
OBJ += $(addprefix $(BUILD)/, $(EXTMOD_SRC_C:.c=.o))
OBJ += $(addprefix $(BUILD)/, $(CRYPTO_SRC_C:.c=.o))

OBJ += $(addprefix $(BUILD)/, $(BSP_SRC:.c=.o))
OBJ += $(addprefix $(BUILD)/, $(LIB_SRC:.c=.o))

OBJ += $(addprefix $(BUILD)/, $(RTOS_SRC:.c=.o))
OBJ += $(addprefix $(BUILD)/, $(SDK_DRIVERS_SRC_C:.c=.o))


# 编译汇编文件
%.o: %.S
	$(CC) $(INC) $(ASMFLAGS) -o $@ $<

# 生成构建工具(通过该工具生成烧录镜像文件)
$(SDK_TOOL_DIR)/wm_tool$(TOOL_EXT): $(SDK_TOOL_DIR)/wm_tool.c
	$(ECHO) "BUILD $@"
	$(Q)gcc -o $@ $<
	$(Q)gcc -o $(SDK_TOOL_DIR)/wm_getver$(TOOL_EXT) $(SDK_TOOL_DIR)/wm_getver.c

# 生成frozen代码(mpy启动时,默认加载并执行,可用于加密部分敏感代码)
$(BUILD)/_frozen_mpy.c: frozentest.mpy $(BUILD)/genhdr/qstrdefs.generated.h
	$(ECHO) "MISC freezing bytecode"
	$(ECHO) "SRC_MOD: ${SRC_MOD}, SRC_MOD_CXX: ${SRC_MOD_CXX}"
	$(Q)$(TOP)/tools/mpy-tool.py -f -q $(BUILD)/genhdr/qstrdefs.preprocessed.h -mlongint-impl=mpz $< > $@

# 将.o连接成elf文件
$(BUILD)/$(ProjName).elf: $(OBJ) $(STARTUP_ASM_SRC:.S=.o)
	$(ECHO) "LINK $@"
	$(Q)$(LD) $(LDFLAGS) -o $@ $^ $(LIBS)
	$(Q)$(OBJDUMP) -D $@ > $(BUILD)/$(ProjName).dis
	$(SIZE) $@
	
# 抽取bin文件
$(BUILD)/$(ProjName).bin: $(BUILD)/$(ProjName).elf
	$(Q)$(OBJCOPY) -O binary $< $@

# 构建烧录镜像
$(IMG_DIR)/$(ProjName).img: $(BUILD)/$(ProjName).bin
	$(ECHO) "Create $@"
	$(Q)$(shell if [ ! -e $(IMG_DIR) ];then mkdir -p $(IMG_DIR); fi)
	$(Q)$(SDK_TOOL_DIR)/wm_tool$(TOOL_EXT) -b ./$(BUILD)/$(ProjName).bin -o ./$(IMG_DIR)/$(ProjName) -it $(img_type) -fc 0 -ra $(run_img_pos) -ih $(run_img_header) -ua $(upd_img_pos) -nh 0  -un 0	 

	$(Q)$(SDK_TOOL_DIR)/wm_tool$(TOOL_EXT) -b $(SDK_TOOL_DIR)/W806_secboot.bin -o ./$(IMG_DIR)/W806_secboot -it 0 -fc 0 -ra $(sec_img_pos) -ih $(sec_img_header) -ua $(upd_img_pos) -nh $(run_img_header)  -un 0  
	$(Q)cat ./$(IMG_DIR)/W806_secboot.img ./$(IMG_DIR)/$(ProjName).img > ./$(IMG_DIR)/$(ProjName).fls

	$(Q)$(SDK_TOOL_DIR)/wm_tool$(TOOL_EXT) -b ./$(IMG_DIR)/$(ProjName).img -o ./$(IMG_DIR)/$(ProjName) -it $(img_type) -fc 1 -ra $(run_img_pos) -ih $(run_img_header) -ua $(upd_img_pos) -nh 0  -un 0
	$(Q)mv ./$(IMG_DIR)/$(ProjName)_gz.img ./$(IMG_DIR)/$(ProjName)_ota.img	
	

# 构建镜像(执行make all即可)
all: $(SDK_TOOL_DIR)/wm_tool$(TOOL_EXT) $(IMG_DIR)/$(ProjName).img

# 清除生成的镜像文件
imgclean: 
	$(ECHO) "Delete $(IMG_DIR)"
#	$(Q)$(shell if [ -e $(BUILD)/$(ProjName).elf ];then rm $(BUILD)/$(ProjName).elf; fi)
	$(Q)$(shell rm $(BUILD)/*.bin $(BUILD)/*.elf )
	$(Q)$(shell if [ -d $(IMG_DIR) ];then rm -rf $(IMG_DIR); fi)


remake: imgclean all



deploy: $(BUILD)/firmware.dfu
	$(ECHO) "Writing $< to the board"
	$(Q)$(PYTHON) $(PYDFU) -u $<

# Run emulation build on a POSIX system with suitable terminal settings
run:
	stty raw opost -echo
	build/firmware.elf
	@echo Resetting terminal...
# This sleep is useful to spot segfaults
	sleep 1
	reset
	
	

.PHONY: all deploy run imgclean remake

include $(TOP)/py/mkrules.mk

	